package com.ihome.android.mod;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import com.ihome.android.analytic.JsonAnalytic;
import com.ihome.android.common.ConfigUtil;
import com.ihome.android.http.HttpActions;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.market2.asynctask.UserLoginTask;
import com.ihome.android.market2.asynctask.UserRegisterTask;
import com.ihome.android.market2.info.LoginResult;
import com.ihome.android.util.Util;
import com.ihome.android.util.exception.ITVException;
import com.ihome.android.util.log.LogUtil;
import com.ihome.android.util.log.UserBehaviorLog;
import com.ihome.android.util.log.UserBehaviorLog.NotifyType;
import com.ihome.android.util.log.UserBehaviorLog.UserBehavior;

public class LoginMod {
	private final static String	TAG					= LoginMod.class.getSimpleName();

	public static final int		FLAG_LOGIN			= 0;
	public static final int		FLAG_QUIT			= 1;
	private final static int	DEFAULT_EXCUTE_TIME	= 10;								// second

	private static boolean		LoginSuccess		= false;
	private static boolean		executeDone			= false;

	public static boolean login(final Context ctx) {
		new Thread() {
			@Override
			public void run() {
				try {
					executeDone = false;
					LoginSuccess = false;
					String desPwd = Util.MD5Encode(HttpActions.DESKey);
					String result = new HttpActions(ctx).loginReq(Util.getOTTUserName(ctx), desPwd, 1);
					LogUtil.v(TAG, "result = " + result);
					if (!TextUtils.isEmpty(result)) {
						LoginResult mLoginResult = JsonAnalytic.getInstance().anslyseLoginResult(result);
						ConfigUtil.SessionKey = mLoginResult.getSessionkey();
						LoginSuccess = true;
					}

				} catch (ITVException e) {
					e.printStackTrace();
					LoginSuccess = false;
				} finally {
					executeDone = true;
				}
			}
		}.start();

		for (int i = 0; i < DEFAULT_EXCUTE_TIME * 2; i++) {
			try {
				Thread.sleep(500);
				if (executeDone) {
					break;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return LoginSuccess;
	}

	public static boolean LogInOrQuit(final Context ctx, final int opflag) {

		new Thread() {
			@Override
			public void run() {
				try {
					String oplog = new HttpActions(ctx).userOplog(opflag);
					LogUtil.v(TAG, "user.oplog = " + oplog);
				} catch (ITVException e) {
					e.printStackTrace();
				}
			}
		}.start();

		return true;

	}
	
	
	/**
	 * 自动注册
	 * 
	 * @param context
	 * 			 -- 上下文，请传入调用该方法的Activity对象
	 * @param clientID
	 *            -- OTT平台返回的用户标示位
	 * @param isOTT
	 *            -- 如果OTT业务认证失败，使用机顶盒Mac注册游客账户
	 */
	public static void autoRegister(Context context, String clientID, boolean isOTT) {
		String userName = "";
		if (isOTT) {
			LogUtil.d(TAG, "Auto register by using ott client id;");
			userName = "OTT" + clientID;
		}
		else {
			if (!TextUtils.isEmpty(Util.getOTTUserName(context.getApplicationContext()))
					&& !TextUtils.isEmpty(Util.getUserPwd(context.getApplicationContext()))) {
				autoLogin(context);
				return;
			}
			userName = "MAC" + Util.getMac();
		}
		String pwd = HttpActions.DESKey;
		Bundle regist = new Bundle();
		regist.putString(HttpKeys.KEY_APPUSERNAME, userName);
		regist.putString(HttpKeys.KEY_PAYPASSWORD, pwd);
		new UserRegisterTask(context).execute(regist);
	}
	
	/**
	 * 自动登陆
	 * 
	 * @param context
	 * 			 -- 上下文，请传入调用该方法的Activity对象
	 */
	public static void autoLogin(Context context) {
		Bundle loginInfo = new Bundle();
		loginInfo.putString(HttpKeys.KEY_APPUSERNAME, Util.getOTTUserName(context.getApplicationContext()));
		loginInfo.putString(HttpKeys.KEY_LOGINPASSWORD, Util.getUserPwd(context.getApplicationContext()));
		loginInfo.putInt(HttpKeys.KEY_OPERATEFLAG, 1);
		new UserLoginTask(context).execute(loginInfo);
	}

}
